package cn.itcast.bos.service.impl;

import cn.itcast.bos.domain.base.Standard;
import cn.itcast.bos.repository.StandardRepository;
import cn.itcast.bos.service.StandardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by Think on 2017/9/14.
 */
@Service
@Transactional
public class StandardServiceImpl implements StandardService {

    @Autowired
    private StandardRepository standardRepository;

    @Override
    @CacheEvict(value="standard",allEntries=true)
    public void save(Standard standard) {
        standardRepository.save(standard);
    }

    @Override
    public List<Standard> findByName(String name) {

        return standardRepository.findByName(name);
    }

    @Cacheable(value="standard",key="#pageable.pageNumber+'_'+#pageable.pageSize")
    public Page<Standard> findPageQuery(Pageable pageable){
        return standardRepository.findAll(pageable);
    }

    @Override
    public void deleteStandardById(String[] idArray) {
        for (String idStr : idArray) {
            standardRepository.delete(Integer.parseInt(idStr));
        }
    }

    @Override
    @Cacheable(value = "standard")
    public List<Standard> findAll() {
        return standardRepository.findAll();
    }
}
